package com.tcoiss.log.controller;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.tcoiss.common.core.domain.R;
import com.tcoiss.common.component.api.model.SchemeLogVo;
import com.tcoiss.log.domain.SchemeLog;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import com.tcoiss.log.service.ISchemeExecuteLogService;
import com.tcoiss.common.core.web.page.TableDataInfo;

/**
 * 方案执行日志Controller
 * 
 * @author zw
 * @date 2021-05-06
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/schemeLog" )
public class SchemeLogController extends BaseController {

    private final ISchemeExecuteLogService iSchemeExecuteLogService;

    /**
     * 查询方案执行日志列表
     */
    @PreAuthorize(hasPermi = "schemeLog:log:list")
    @GetMapping("/list")
    public TableDataInfo list(SchemeLog schemeLog) {
        startPage();
        List<SchemeLog> list = iSchemeExecuteLogService.queryList(schemeLog);
        return getDataTable(list);
    }

    /**
     * 获取最新的方案执行时间
     */
    @GetMapping(value = "/getSchemeLogNewTime/{executeNumber}")
    public R<Date> getSchemeLogNewTime(@PathVariable("executeNumber") String executeNumber){
        return R.ok(iSchemeExecuteLogService.getSchemeLogNewTime(executeNumber));
    }

    /**
     * 获取方案执行日志详细信息
     */
    @PreAuthorize(hasPermi = "schemeLog:log:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        SchemeLog log = iSchemeExecuteLogService.getById(id);
        return AjaxResult.success(log);
    }

    /**
     * 获取作业数量与方案名称
     */
//    @PostMapping(value = "/queryWork" )
//    public SchemeLogVo queryWork(@RequestBody SchemeLogVo sel) {
//        return iSchemeExecuteLogService.queryScheme(sel);
//    }

    /**
     * 新增方案执行日志
     */
    //@PreAuthorize(hasPermi = "schemeLog:log:add" )
    //@Log(title = "方案执行日志" , businessType = BusinessType.INSERT)
    @PostMapping("/saveSchemeLog")
    public R<Boolean> saveSchemeLog(@RequestBody SchemeLogVo schemeLogVo) {
        String json = JSONObject.toJSONString(schemeLogVo);
        SchemeLog schemeLog = JSONObject.parseObject(json,SchemeLog.class);
        return R.ok(iSchemeExecuteLogService.save(schemeLog));
    }

    /**
     * 检查方案执行日志进行预警
     */
    @PostMapping("/checkSchemeLog" )
    public AjaxResult checkSchemeLog() {
        return toAjax(iSchemeExecuteLogService.checkSchemeLog() ? 1 : 0);
    }

    @PreAuthorize(hasPermi = "schemeLog:log:remove")
    @Log(title = "方案执行日志", businessType = BusinessType.CLEAN)
    @GetMapping("/clean")
    public AjaxResult clean(SchemeLog schemeLog) {
        List<SchemeLog> schemeLogList = iSchemeExecuteLogService.queryList(schemeLog);
        List<Long> ids = schemeLogList.stream().map(SchemeLog :: getId).collect(Collectors.toList());
        return toAjax(iSchemeExecuteLogService.removeByIds(ids) ? 1:0);
    }


}
